from typing import List


class Solution:
    def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
        ans = []
        i = 0
        j = 0
        length = 0
        while j < len(words):
            if length + len(words[j]) <= maxWidth:
                length += len(words[j])
                j += 1
            else:
                space = maxWidth - length
                space_cnt = [space // (j - i - 1) for _ in range(j - i - 1)]
                sums = space // (j - i - 1) * (j - i - 1)
                for k in range(len(space_cnt)):
                    if sums < space:
                        sums += 1
                        space_cnt[k] += 1
                    else:
                        break
                string = words[i]
                for k in range(len(space_cnt)):
                    string += space_cnt[k] * ' '
                    string += words[i + k + 1]
                ans.append(string)
                i = j
                length = 0
        return ans

if __name__ == '__main__':
    s = Solution()
    r = s.fullJustify( words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16)